<template>

  <div class="exBannerContainer">
    <div class="option">
      <el-button
        type="success"
        size="small"
        @click="addexBanner()"
      >新增</el-button>
      <el-button
        type="primary"
        size="small"
        @click="Refresh()"
      >刷新</el-button>
    </div>
    <el-table
      :data="List"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="index"
        label="序号"
      ></el-table-column>

      <el-table-column
        prop="name"
        label="运营商"
      ></el-table-column>

      <el-table-column label="图片">
        <template slot-scope="scope">
          <img
            :src="`http://www.jfqiandai.com/file${scope.row.pic}`"
            style="width:80px"
          >
        </template>
      </el-table-column>

      <el-table-column
        prop="url"
        label="url"
      ></el-table-column>

      <el-table-column
        prop="createTime"
        label="创建时间"
      ></el-table-column>

      <el-table-column
        fixed="right"
        label="操作"
        width="240"
      >
        <template slot-scope="scope">
          <!-- <el-button @click="handleClick(scope.row)" type="primary" size="small">查看</el-button> -->
          <el-button
            @click="EditClick(scope.row)"
            type="primary"
            plain
            size="small"
          >编辑</el-button>
          <el-button
            @click="DeleteClick(scope.row)"
            type="danger"
            size="small"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="paginat">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage4"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 弹框 -->
    <el-dialog
      title="提示"
       @close="Cancel"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form
        label-width="70px"
        class="demo-ruleForm"
      >
        <el-form-item label="运营商">
          <el-select
            v-model="carrierId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in carrierList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="图片">
          <el-upload
            list-type="picture-card"
            action="http://www.jfqiandai.com/img/upload"
            :file-list="fileList"
            :on-success="onSuccess"
            :on-remove="handleRemove"
            :limit="1"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="链接地址">
          <el-input v-model="url"></el-input>
        </el-form-item>

        <div style="text-align:right;">
          <el-button @click="Cancel()">取 消</el-button>
          <el-button
            type="primary"
            @click="isOkClick()"
          >确 定</el-button>
        </div>

      </el-form>
    </el-dialog>
  </div>

</template>


<script>
export default {
  data() {
    return {
      flag: 0,
      val: 1,
      currentPage4: 1,
      total: 0,
      List: [],
      fileList: [],
      EditId: "",
      dialogVisible: false,
      carrierList: [],
      carrierId: "",
      pic: "",
      url: ""
    };
  },
  created() {
    this.getList();
    this.getcarrierName();
  },
  methods: {
    //确定
    isOkClick() {
      if (this.flag == 1) {
        let url = "/exchangeBanner/addOne";
        let params = {
          carrierId: this.carrierId, //运营商id
          pic: this.pic,
          url: this.url
        };
        console.log(params);
        this.httpPost(url, params, res => {
          if (res.code == 200) {
            this.dialogVisible = false;
            this.getList();
          }
        });
      } else {
        let url = "/exchangeBanner/editOne";
        let params = {
          id: this.EditId,
          carrierId: this.carrierId, //运营商id
          pic: this.pic,
          url: this.url
        };
        this.httpPut(url, params, res => {
          if (res.code == 200) {
            this.dialogVisible = false;
            this.getList();
          }
        });
      }
    },
        //取消
    Cancel() {
      this.dialogVisible = false;
      this.fileList = [];
    },
    //图片上传
    onSuccess(Response, file, fileList) {
      this.pic = file.response.data.path;
    },
    handleRemove(file, fileList) {
      this.pic = "";
      this.fileList = [];
    },
    //新增
    addexBanner() {
      this.flag = 1;
      this.dialogVisible = true;
      this.carrierId = "";
      this.url = "";
      this.pic = "";
      this.fileList = [];
    },
    //编辑
    EditClick(row) {
      this.flag = 2;
      this.dialogVisible = true;
      this.EditId = row.id;
      this.carrierId = row.carrierId;
      this.url = row.url;
      this.pic = row.pic;
      if (this.pic) {
         this.fileList.push({ url: "http://www.jfqiandai.com/file" + row.pic });
      } else {
        this.fileList = [];
      }
    },
    //分页
    handleCurrentChange(val) {
      this.val = val;
      this.getList();
      console.log(`当前页: ${val}`);
    },
    //删除
    DeleteClick(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let url = "/exchangeBanner/editOne";
          let params = {
            id: row.id,
            status: 1
          };
          this.httpPut(url, params, res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功"
              });
              this.dialogVisible = false;
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //获取数据
    getList() {
      let url = "/exchangeBanner/getList";
      let params = {
        pageNum: this.val,
        pageSize: 10
      };
      this.httpPost(url, params, res => {
        if (res.code == 200) {
          this.List = res.data.list;
          this.total = res.data.total;
          this.List.forEach((element, i) => {
            this.List[i].index = (this.val - 1) * 10 + i + 1;
          });
        } 
      });
    },
    //刷新
    Refresh() {
      this.getList();
    },
    //获取运营商名称
    getcarrierName() {
      let url = "/carrier/findList";
      let params = {
        pageNum: "",
        pageSize: ""
      };
      this.httpPost(url, params, res => {
        if (res.code == 200) {
          this.carrierList = res.data.list;
        } else {
          console.log("查询失败");
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.exBannerContainer {
  .option {
    padding: 10px;
  }
  //弹框
  .row {
    padding: 10px;
    .rowTil {
      display: block;
      padding: 10px 0;
    }
  }
}
</style>
